package net.adetech.port.start;


import net.adetech.port.job.SyncJob;
import net.adetech.port.service.impl.SyncServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.ComponentScan;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


@SpringBootApplication
@ComponentScan(basePackages = {"net.adetech.port.*"})
public class Application implements EmbeddedServletContainerCustomizer {

    private static int port = 8090;

    public static void main(String[] args) {
        if (null != args && args.length > 0) {
            port = Integer.parseInt(args[0]);
        }

        String fullPath = Application.class.getProtectionDomain().getCodeSource().getLocation().getFile();
        if (fullPath.indexOf("sync_ticket-1.0.0.jar") > -1) {
            String jarPath = fullPath.substring(5, fullPath.indexOf("sync_ticket-1.0.0.jar"));
            String configFilePath = jarPath + "config.ini";
            initByConfigFile(configFilePath);
        }
        else {
            String configFilePath = fullPath + "config.ini";
            initByConfigFile(configFilePath);
        }

        SpringApplication.run(Application.class, args);

    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
        configurableEmbeddedServletContainer.setPort(port);
    }

    public static void initByConfigFile(String configFilePath) {
        Map<String, String> map = new HashMap<String,String>();

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(configFilePath)),
                    "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                if (lineTxt.indexOf("=") <= 0) {
                    continue;
                }
                String key = lineTxt.split("=")[0];
                String value = lineTxt.split("=")[1];
                if ("ftp.ip".equals(key)) {
                    SyncJob.ftpIp = value;
                    System.out.println("[INFO]ftp服务器ip\t--> " + value);
                }
                if ("ftp.port".equals(key)) {
                    SyncJob.ftpPort = Integer.parseInt(value);
                    System.out.println("[INFO]ftp服务器端口\t--> " + value);

                }
                if ("ftp.username".equals(key)) {
                    SyncJob.ftpUser = value;
                    System.out.println("[INFO]ftp服务器用户名\t--> " + value);

                }
                if ("ftp.password".equals(key)) {
                    SyncJob.ftpPassword = value;
                    System.out.println("[INFO]ftp服务器密码\t--> " + value);

                }
                if ("ftp.localActiveMode".equals(key)) {
                    SyncJob.localActiveMode = value;
                    System.out.println("[INFO]ftp服务器主动模式\t--> " + value);

                }
                if ("cache.filepath".equals(key)) {
                    SyncJob.filepath = value;
                    System.out.println("[INFO]缓存文件存储路径\t--> " + value);

                }
                if ("job.interval".equals(key)) {
                    SyncJob.interval = Integer.parseInt(value);
                    System.out.println("[INFO]上传文件时间间隔\t--> " + value + "秒");
                }
                if ("database".equals(key)) {
                    SyncServiceImpl.database = value;
                    System.out.println("[INFO]数据库地址\t--> " + value + "");
                }
                if ("start".equals(key)) {
                    if ("cdg".equalsIgnoreCase(value))
                    {
                        SyncServiceImpl.sfg = "长岛";
                        System.out.println("[INFO]始发港\t--> 长岛");
                    }

                    if ("plg".equalsIgnoreCase(value))
                    {
                        SyncServiceImpl.sfg = "蓬莱";
                        System.out.println("[INFO]始发港\t--> 蓬莱");
                    }

                }
            }
            br.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }
    }

}